Conditions | 1 |
Paths | 24 |
Total Lines | 115 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 17 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var TableHelper = { |
||
251 | function initInstance(){ |
||
252 | // Singleton |
||
253 | // Private methods and variables |
||
254 | function BuildRequest(rq, crntTableId, strDesc){ |
||
255 | rq.tableId = crntTableId; |
||
256 | TableHelper.BuildRequest.Sort(rq, strDesc); |
||
257 | TableHelper.BuildRequest.Filter(rq); |
||
258 | }; |
||
259 | var tail = null; |
||
260 | function Init(tableId){ |
||
261 | var tContainer = document.getElementById(tableId); |
||
262 | if(!TableHelper.IePrior(9)){ |
||
263 | TableHelper.Init.SetColumnsHoverEffect(tContainer, tableId); |
||
264 | } |
||
265 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
266 | TableHelper.ProcessPaginationLinks(tfoot); |
||
267 | } |
||
268 | function LoadData(tableContainer, rq){ |
||
269 | if(tail!==null){ tail.abort();} |
||
270 | TableHelper.LoadData.SetVisability(tableContainer, false); |
||
271 | var xmlhttp = window.XMLHttpRequest ? |
||
272 | new XMLHttpRequest() : /* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
273 | new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
274 | xmlhttp.onreadystatechange = function(){ |
||
275 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
276 | Draw(tableContainer, JSON.parse(xmlhttp.responseText)); |
||
277 | TableHelper.LoadData.SetVisability(tableContainer, true); |
||
278 | instance.LoadEndCalback(tableContainer); |
||
279 | } |
||
280 | }; |
||
281 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
282 | xmlhttp.send(); |
||
283 | tail = xmlhttp; //put at tail to can abort later any previous |
||
284 | } |
||
285 | function Draw(tableContainer, d){ |
||
286 | TableHelperDraw_Section(tableContainer, d.body); |
||
287 | TableHelperDraw_Section(tableContainer, d.footer, "tfoot"); |
||
288 | if(instance.rq !== null){ |
||
289 | var hover = document.getElementById(instance.rq.tableId) |
||
290 | .getElementsByTagName("th")[instance.rq.colNo].lang; |
||
291 | if(hover){ |
||
292 | instance.ColumnHover(tableContainer, instance.rq.colNo); |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 | |||
297 | var GoPageGetNo = TableHelper.GoPage.GetNo; |
||
298 | var getParent = TableHelper.GetParent; |
||
299 | var RequestToUrl = TableHelper.RequestToUrl; |
||
300 | |||
301 | return { |
||
302 | rq: null, |
||
303 | strAsc: String.fromCharCode(9650), //▲ |
||
304 | strDesc: String.fromCharCode(9660),//▼ |
||
305 | ColumnHover: TableHelper.ColumnHover, //function(tableContainer, index) |
||
306 | Export: function(lnk, eType){ |
||
307 | var request = {}; |
||
308 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
309 | BuildRequest(request, crntTableId, this.strDesc); |
||
310 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
311 | eType : |
||
312 | "csv"; |
||
313 | window.open(RequestToUrl(request)); |
||
314 | return false; |
||
315 | }, |
||
316 | Filter: function(field){ |
||
317 | var crntTableId = TableHelper.Filter.GetTableId(field); |
||
318 | if(crntTableId !== null){ |
||
319 | var request = {}; |
||
320 | var exRq = this.rq; |
||
321 | BuildRequest(request, crntTableId, this.strDesc); |
||
322 | if(exRq === null || |
||
323 | request.filter !== exRq.filter || |
||
324 | request.filterBy !== exRq.filterBy |
||
325 | ){ |
||
326 | LoadData(crntTableId, request); |
||
327 | } |
||
328 | } |
||
329 | }, |
||
330 | GoPage: function(lnk){ |
||
331 | var request = {}; |
||
332 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
333 | BuildRequest(request, crntTableId, this.strDesc); |
||
334 | request.pageNo = GoPageGetNo(lnk, crntTableId); |
||
335 | LoadData(crntTableId, request); |
||
336 | return false; |
||
337 | }, |
||
338 | init: Init, //funcion(tableId) |
||
339 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
340 | ReloadData: function(tableId){ |
||
341 | var request = {}; |
||
342 | BuildRequest(request, tableId, this.strDesc); |
||
343 | LoadData(tableId, request); |
||
344 | }, |
||
345 | Sort: function(colNo, lnk){ |
||
346 | var request = {}; |
||
347 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
348 | BuildRequest(request, crntTableId, this.strDesc); |
||
349 | if(Math.round(colNo) === request.colNo){ |
||
350 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
351 | }else{ |
||
352 | request.colNo = Math.round(colNo); |
||
353 | request.colOrd = "asc"; |
||
354 | } |
||
355 | LoadData(crntTableId, request); |
||
356 | /* Clear and add new sort arrow */ |
||
357 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
358 | var length = headSpans.length; |
||
359 | for(var i = 0; i < length; i++){ |
||
360 | headSpans[i].innerHTML = ""; |
||
361 | } |
||
362 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
363 | } |
||
364 | }; |
||
365 | } |
||
366 | return { |
||
377 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: